package com.qf.shiro;

import com.qf.common.http.ResultCode;

import com.qf.entiy.dto.MerchantUser;
import com.qf.entiy.po.LoginMerchant;
import com.qf.service.impl.MerchantUserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

public class CustomRealm extends AuthorizingRealm {

    @Autowired
    private MerchantUserService merchantUserService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return new SimpleAuthorizationInfo();
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        String username = token.getUsername();
        String password = new String(token.getPassword());
        MerchantUser merchantUser = merchantUserService.getByUserName(username);
        if (merchantUser == null) {
            throw new RuntimeException(ResultCode.LOGIN_ERROR.getDescription());
        }
        if (!merchantUser.getPassword().equals(password)) {
            throw new RuntimeException(ResultCode.LOGIN_ERROR.getDescription());
        }
        LoginMerchant loginMerchant = new LoginMerchant();
        BeanUtils.copyProperties(merchantUser, loginMerchant);
        return new SimpleAuthenticationInfo(loginMerchant, merchantUser.getPassword(), merchantUser.getUserName());
    }
}